/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.EditUser;
import controller.ListUsers;
import entities.Users;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@SessionScoped
public class ListUsersBean {

    private List<Users> use;
    private Users user;

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public List<Users> getUse() {
        return getListUser();
    }

    public void setUse(List<Users> use) {
        this.use = use;
    }

    /**
     * Creates a new instance of ListUsersBean
     */
    public ListUsersBean() {
    }

    public List<Users> getListUser() {
        ListUsers list = new ListUsers();
        List<Users> users = list.getUsers();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        LoginBean myObject = (LoginBean) app.createValueBinding("#{loginBean}").getValue(facesContext);
        Users s = myObject.getUser();
        String name = s.getUsername();
        List<Users> u = new ArrayList<Users>();
        for (Users us : users) {
            if (!us.getUsername().equals(name)) {
                u.add(us);
            }
        }
        return u;
    }

    public String activate() {
        user.setStatus(Boolean.TRUE);
        EditUser eu = new EditUser();
        eu.resultEdit(user);
        return "userInfo.xhtml?faces-redirect=true";
    }

    public String deactivate() {
        user.setStatus(Boolean.FALSE);
        EditUser eu = new EditUser();
        eu.resultEdit(user);
        return "userInfo.xhtml?faces-redirect=true";
    }
}
